package com.edu.hre.db.sql.admin;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import com.edu.hre.common.utils.CommonUtils;
import com.edu.hre.db.pojo.HreEducation;
import com.edu.hre.db.service.BaseService;
import com.edu.hre.db.utils.Compare;
import com.edu.hre.struts.form.admin.EducationForm;
import com.edu.hre.struts.form.admin.EducationListForm;

public class EducationDAOService implements Serializable {
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 3394301416150421253L;
	public static final String BEAN_NAME = "educationDAOService";
	private BaseService baseService  = null;

	public void setBaseService(BaseService baseService) {
		this.baseService = baseService;
	}

	public ArrayList<EducationForm> getEducationListInfo(int limit, int offset){

		List<HreEducation> hreEducations = baseService.getPojos(null, -1, null, new String[]{"dateCreated"}, new Boolean[]{true}, limit, offset, HreEducation.class);
		ArrayList<EducationForm> educations = new ArrayList<EducationForm>();
		
		if (hreEducations == null) {
			return educations;
		}
		for (HreEducation hreEdu: hreEducations) {
			
			try {

				EducationForm education = new EducationForm();
				education.setId(hreEdu.getId() + "");
				education.setName(CommonUtils.normalizeString(hreEdu.getName()));
				
				educations.add(education);
			} catch (Exception e) {
				CommonUtils.println(e.getMessage());
			}
		}
		
		return educations;
	}

	public int countEducations(){

		int count = baseService.countByColumns(null, null, null, "id", HreEducation.class);
		return count < 0 ? 0 : count;
	}

	public void deleteSkills(EducationListForm form){

		ArrayList<EducationForm> info = (ArrayList<EducationForm>) form.getAllEducations();
		
		for (int i = 0; i < info.size(); i++) {
			
			try {

				EducationForm edu = info.get(i);
				if (edu.isSelected()) {
					baseService.deleteFirstPojo("id", Compare.EQUAL, Integer.parseInt(edu.getId()), null, null, 0, HreEducation.class);
				}
			} catch (Exception e) {
				CommonUtils.println(e.getMessage());
			}
		}
	}
}
